package com.example.ex09;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
	private List<HashMap<String,String>> mDataList = new ArrayList<HashMap<String,String>>();
	private ListView mListView;
	Button addContacBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}
	
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		
	}


	private void init(){
		
		addContacBtn = (Button)findViewById(R.id.createBtn);
		addContacBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, AddContact.class);
				startActivity(intent);
			}
		});
		
		mListView = (ListView)findViewById(R.id.listView1);
		setData();
		SimpleAdapter listItemAdapter = new SimpleAdapter( this, mDataList,R.layout.contact,
											new String[]{"phone","gender","name","info"},
											new int[]{R.id.phone_in_list,R.id.gender_in_list,R.id.name_in_list,R.id.info_in_list});
		mListView.setAdapter( listItemAdapter );
		mListView.setOnItemLongClickListener( new OnItemLongClickListener() {

			

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("phone", ((TextView)arg1.findViewById(R.id.phone_in_list)).getText().toString());
				bundle.putString("name", ((TextView)arg1.findViewById(R.id.name_in_list)).getText().toString());
				bundle.putString("info", ((TextView)arg1.findViewById(R.id.info_in_list)).getText().toString());
				
				DiyDialog dialog = new DiyDialog(MainActivity.this,bundle , new DiyDialog.PriorityListener() {
					
					@Override
					public void refreshPriorityUI() {
						// TODO Auto-generated method stub
						System.out.println("refreshing UI!");
						mDataList.clear();
						setData();
						SimpleAdapter listItemAdapter = new SimpleAdapter( MainActivity.this, mDataList,R.layout.contact,
															new String[]{"phone","gender","name","info"},
															new int[]{R.id.phone_in_list,R.id.gender_in_list,R.id.name_in_list,R.id.info_in_list});
						mListView.setAdapter( listItemAdapter );
					}
				});
				dialog.show();
				return false;
			}
		});
		
	}
	
	private void setData(){
		SQLiteDatabase db = ( new DataBaseHelper(MainActivity.this, AddContact.TABLE_NAME) ).getReadableDatabase();
		Cursor c = db.query("user",null,null,null,null,null,null);
		while( c.moveToNext() ){
			System.out.println( "acquiring data!" );
			HashMap<String, String>map = new HashMap<String,String>();
			map.put("name", c.getString(0) );
			map.put("gender", c.getString(2) );
			map.put("phone", c.getString(1) ); 
			map.put("info", c.getString(3) ); 
			mDataList.add(map);
		}
		c.close();
		db.close();
	}
}
